package com.cactus.auth2.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cactus.auth2.entity.UserInfo;
import com.cactus.auth2.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;
 

@Component
public class UserAuthService implements UserDetailsService {
    @Autowired
    private UserMapper userMapper;
 
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//        UserInfo user = userMapper.getByUsername(username);

        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        QueryWrapper<UserInfo> eq = queryWrapper.eq("user_name", username);
        UserInfo user = userMapper.selectOne(eq);
        if(user == null){
            throw new UsernameNotFoundException("not found the user:"+username);
        }else{
            return user;
        }
//        if("admin".equals(username)){
//            BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
//            return new UserInfo(1, "admin", encoder.encode("123456"));
//        }else{
//            throw new UsernameNotFoundException("not found the user:"+username);
//        }
    }
}